﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FT.TouchScreen.Terminal.ControlInterface;


namespace FT.TouchScreen.Terminal.Controls
{
    /// <summary>
    /// 可滑动拖动控件
    /// </summary>
    public partial class DragPanel : UserControl,IDragPanel
    {
        public DragPanel()
        {
            InitializeComponent();
        }

        private DragDirection direction = DragDirection.Vertical;

        /// <summary>
        /// 控件拖动方向
        /// </summary>
        public DragDirection Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        private int mouseDownX = -1;
        private int mouseDownY = -1;
        private void DragPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
#if DEBUG
                Console.WriteLine("滑动控件鼠标按下！" + e.X + ";" + e.Y);
#endif
                MouseDownXY(e.X, e.Y);
            }
        }


        private void MouseDownXY(int x,int y)
        {
            mouseDownX = x;
            mouseDownY = y;
        }

        private void MouseMoveXY(int x, int y)
        {
            if (DragDirection.Horizontal == direction)
            {
                Control parent = this.Parent;
                int width = parent.Width;
                int oldX = this.Location.X;
                //向左拖动说明定位X减少
                int distance = x - mouseDownX;
                int newX = oldX + distance;
                if (newX < 0 && newX < width - this.Width)
                {
                    newX = width - this.Width;
                }
                else if (newX > 0)
                {
                    newX = 0;
                }
                this.Location = new Point(newX, this.Location.Y);

            }
            else
            {
                Control parent = this.Parent;
                int height = parent.Height;
                int oldY = this.Location.Y;
                //向上拖动说明定位Y减少
                int distance = y - mouseDownY;

                int newY = oldY + distance;
#if DEBUG
                Console.WriteLine("父控件高度为："+height.ToString()+"移动的鼠标位置为："+y+"移动前定位Y的位置为："+oldY.ToString()+"移动距离为："+distance+"移动后新的定位位置为："+newY.ToString());
#endif
           
                if (newY < 0 && newY < height - this.Height)
                {
                    newY = height - this.Height;
                }
                else if (newY > 0)
                {
                    newY = 0;
                }
     this.Location = new Point(this.Location.X, newY);

            }
        }

        private void MouseUpXY(int x, int y)
        {
            
            mouseDownX = mouseDownY = -1;
        }
        private void DragPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
#if DEBUG
                Console.WriteLine("滑动控件鼠标放开！" + e.X + ";" + e.Y);
#endif
               // this.MouseUpXY(e.X, e.Y);
            }
        }

        public void AddControl(Control ctr)
        {
            this.AddEventToControl(ctr);
            this.Controls.Add(ctr);
        }

        private void AddEventToControl(Control ctr)
        {
            if (ctr.Controls.Count == 0)
            {
                ctr.MouseDown += new MouseEventHandler(ctr_MouseDown);
                //ctr.MouseUp += new MouseEventHandler(ctr_MouseUp);
                ctr.MouseMove += new MouseEventHandler(ctr_MouseMove);
            }
            else
            {
                foreach(Control  ctrTmp in ctr.Controls)
                {
                   this.AddEventToControl(ctrTmp);
                }
            }
        }

        void ctr_MouseMove(object sender, MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
                if (e.Button == MouseButtons.Left)
                {
#if DEBUG
                    Console.WriteLine("滑动控件子控件鼠标移动！" + e.X + ";" + e.Y);
#endif
                    Point p = this.PointToClient(Control.MousePosition);
#if DEBUG
                    Console.WriteLine("相对坐标！" + p.X + ";" + p.Y);
#endif
                    this.MouseMoveXY(p.X, p.Y);
                }
            }
        }

        void ctr_MouseUp(object sender, MouseEventArgs e)
        {
#if DEBUG
            Console.WriteLine("滑动控件子控件鼠标放开！"+e.X+";"+e.Y);
#endif
            Point p = this.PointToClient(Control.MousePosition);
#if DEBUG
            Console.WriteLine("相对坐标！" + p.X + ";" + p.Y);
#endif
            this.MouseUpXY(p.X, p.Y);
           // throw new NotImplementedException();
        }

        void ctr_MouseDown(object sender, MouseEventArgs e)
        {
           // throw new NotImplementedException();
#if DEBUG
            Console.WriteLine("滑动控件子控件鼠标按下！" + e.X + ";" +e.Y);
#endif
            Point p = this.PointToClient(Control.MousePosition);
#if DEBUG
            Console.WriteLine("相对坐标！" + p.X + ";" + p.Y);
#endif
            this.MouseDownXY(p.X, p.Y);
        }





        #region IDragPanel 成员

        public event EventHandler DragMouseDown;

        public event EventHandler DragMouseUp;

        #endregion

        private void DragPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.MouseMoveXY(e.X, e.Y);
            }
        }
    }

    public enum DragDirection
    {
        /// <summary>
        /// 水平拖曳
        /// </summary>
        Horizontal,
        /// <summary>
        /// 垂直拖曳
        /// </summary>
        Vertical
    }
}
